package so.sao.shop.supplier.web;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.config.Constant;
import so.sao.shop.supplier.domain.DistributionScope;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.input.DistributionScopeInput;
import so.sao.shop.supplier.service.DistributionScopeService;
import so.sao.shop.supplier.util.JwtTokenUtil;
import so.sao.shop.supplier.util.Ognl;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gxy on 2017/9/18.
 */
@RestController
@RequestMapping("/distributionscope")
public class DistributionScopeController {
    @Autowired
    DistributionScopeService distributionScopeService;

    /**
     * 增加配送范围
     * @param request request
     * @param distributionScopeInput 配送范围入参信息
     * @return Result
     * @throws Exception Exception
     */
    @PostMapping("/create")
    public Result create(@RequestBody @Valid DistributionScopeInput distributionScopeInput) throws Exception {
        //1.取出当前登录用户
        User user = JwtTokenUtil.getCurrentUser();
        if (Ognl.isEmpty( user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        //增加配送范围
        boolean isCreate = distributionScopeService.createDistributionScope(user.getAccountId(), distributionScopeInput);
        if (!isCreate){
            return Result.fail("该配送范围已存在或添加失败!");
        }
        return Result.success(Constant.MessageConfig.MSG_SUCCESS);
    }

    /**
     * 分页获取供应商配送范围列表
     * @param request request
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @return Result
     * @throws Exception Exception
     */
    @GetMapping("/queryAll")
    public Result queryAll( Integer pageNum, Integer pageSize) throws Exception {
        //1.取出当前登录用户
        User user = JwtTokenUtil.getCurrentUser();
        if (Ognl.isEmpty( user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        List<DistributionScope> dataList = distributionScopeService.queryAll(user.getAccountId(), pageNum, pageSize);
        return Result.success(Constant.MessageConfig.MSG_SUCCESS, new PageInfo<>(dataList));
    }

    /**
     * 获取单个配送范围信息
     * @param id id
     * @return Result
     * @throws Exception Exception
     */
    @GetMapping("/query/{id}")
    public Result query(@PathVariable Integer id) throws Exception{
        DistributionScope distributionScope = distributionScopeService.query(id);
        return Result.success(Constant.MessageConfig.MSG_SUCCESS, distributionScope);
    }

    /**
     * 更新某条配送范围信息
     * @param distributionScopeInput 配送范围实体
     * @return Result
     * @throws Exception Exception
     */
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody @Valid DistributionScopeInput distributionScopeInput) throws Exception {
        //1.取出当前登录用户
        User user = JwtTokenUtil.getCurrentUser();
        if (Ognl.isEmpty( user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        boolean isUpdate = distributionScopeService.update(user.getAccountId(),id,distributionScopeInput);
        if (!isUpdate){
            return Result.fail("该配送范围已存在或修改失败!");
        }
        return Result.success(Constant.MessageConfig.MSG_SUCCESS);
    }

    /**
     * 删除某条记录
     * @param id id
     * @return Result
     * @throws Exception Exception
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        //1.取出当前登录用户
        User user = JwtTokenUtil.getCurrentUser();
        if (Ognl.isEmpty( user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return distributionScopeService.delete(id,user.getAccountId()) == true ? Result.success(Constant.MessageConfig.MSG_SUCCESS) : Result.fail("配送规则正在使用中!");
    }
}
